package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.UIManager;

import com.sun.corba.se.impl.protocol.giopmsgheaders.KeyAddr;

import model.Service;

public class LogON extends JFrame {
	private JComboBox userCOm;
	private JPasswordField passField;
	private JButton btnLog, btnChangePassword;
	private JLabel lblUser, lblPass, lblImage;

	private Controller controller = new Controller();
	private Service service = Service.getInstance();
	private DefaultComboBoxModel comboModel;
	private boolean user = true;

	public LogON() {
		this.setTitle("System Login");
		this.setSize(380, 200);
		this.setLocation(350, 200);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		addKeyListener(new KeyController());
		setFocusable(true);

		comboModel = new DefaultComboBoxModel();
		comboModel.addElement("Inventory Manager");
		comboModel.addElement("Erector");

		userCOm = new JComboBox(comboModel);
		this.add(userCOm);
		userCOm.setSize(200, 25);
		userCOm.setLocation(120, 55);
		userCOm.addActionListener(controller);
		userCOm.setToolTipText("Choose your account type");

		lblUser = new JLabel(" Account Type: ");
		this.add(lblUser);
		lblUser.setSize(100, 25);
		lblUser.setLocation(40, 55);

		lblImage = new JLabel("Inventory Management System");
		this.add(lblImage);
		lblImage.setSize(380, 45);
		lblImage.setFont(new Font("Arial", 0, 15));
		lblImage.setLocation(15, 5);
		lblImage.setIcon(new ImageIcon("gui/SiteLogo.png"));
	//	lblImage.setForeground(Color.red);
	//	lblImage.setBorder(BorderFactory.createLineBorder(Color.red));

		passField = new JPasswordField();
		this.add(passField);
		passField.setSize(200, 25);
		passField.setLocation(120, 85);
		passField.setToolTipText("Type in your password here");

		lblPass = new JLabel(" Password: ");
		this.add(lblPass);
		lblPass.setSize(100, 25);
		lblPass.setLocation(40, 85);

		btnChangePassword = new JButton("Change Password");
		this.add(btnChangePassword);
		btnChangePassword.setSize(135, 25);
		btnChangePassword.setLocation(185, 130);
		btnChangePassword.addActionListener(controller);
		btnChangePassword.setToolTipText("Change your password");

		btnLog = new JButton("Log In");
		this.add(btnLog);
		btnLog.setSize(135, 25);
		btnLog.setLocation(40, 130);
		btnLog.addActionListener(controller);
		btnLog.setToolTipText("Log into the system");
	}
	
	@SuppressWarnings("deprecation")
	public void userLogin(){
		String password = passField.getText().trim();

		if (service.login(userCOm.getSelectedItem().toString(),
				password)) {
			GuiDesignPlan workIM = new GuiDesignPlan();
			workIM.setVisible(true);
			workIM.setCurrentUser(user);
			setVisible(false);
		} else {
			JOptionPane.showMessageDialog(null, "Incorrect password for this account type!", "Incorrect password!", JOptionPane.INFORMATION_MESSAGE);
		}
	
	}

	public class Controller implements ActionListener {
		ArrayList<String> passwords = new ArrayList<String>();

		public void actionPerformed(ActionEvent e) {
			 if (e.getSource() == userCOm) {
			   if (userCOm.getSelectedIndex() == 0) user = true;
			   else  if (userCOm.getSelectedIndex() == 1) user = false;
			 }
			if (e.getSource() == btnLog) {
				userLogin();
			}
			if (e.getSource() == btnChangePassword) {
				ChangePasswordDialog updateDialog = new ChangePasswordDialog(LogON.this);
				updateDialog.setVisible(true);
				updateDialog.dispose();
				passField.setText("");
			}
		}
	}
	
	
	
	public class KeyController implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				userLogin();

			}
			if ( e.getKeyCode() == KeyEvent.VK_ESCAPE){
				int answer = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to exit?", "Exit",
						JOptionPane.YES_NO_OPTION);
				if (answer==JOptionPane.YES_OPTION){
					System.exit(0);
				}
			}

		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	}
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.out.println("Error setting look and feel: " + e.getMessage());
		}
		LogON log=new LogON();
		log.setVisible(true);
		log.requestFocus();
	}


}
